
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>class和继承 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="this.html" />
    
    
    <link rel="prev" href="模块化.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >class和继承</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="class&#x548C;&#x7EE7;&#x627F;">class&#x548C;&#x7EE7;&#x627F;</h1>
<p>ES6&#x4E4B;&#x524D;, &#x4F7F;&#x7528;&#x51FD;&#x6570;&#x6A21;&#x62DF;&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x5BF9;&#x8C61;:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name, age</span>) </span>{
            <span class="hljs-keyword">this</span>.name = name;
            <span class="hljs-keyword">this</span>.age = age;
        }
        Person.prototype.showName = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`my name is: <span class="hljs-subst">${this.name}</span>`</span>);
        };
        Person.prototype.showAge = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`my age is: <span class="hljs-subst">${this.age}</span>`</span>);
        };
        <span class="hljs-comment">// &#x6DFB;&#x52A0;&#x65B9;&#x6CD5;&#x4E5F;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199;:</span>
        <span class="hljs-comment">/**
        Object.assign(Person.prototype, {
            showName() {
                console.log(`my name is: ${this.name}`);
            },
            showAge() {
                console.log(`my age is: ${this.age}`);
            }
        });
         */</span>
        <span class="hljs-keyword">let</span> p1 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">&apos;daliu&apos;</span>, <span class="hljs-number">18</span>);
        p1.showName();
        p1.showAge();
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>ES6 &#x5B9E;&#x73B0;&#x4E86;&#x5176;&#x4FDD;&#x7559;&#x5B57; class &#x4F5C;&#x4E3A;&#x7C7B;&#x7684;&#x58F0;&#x660E;:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
            <span class="hljs-keyword">constructor</span>(name, age) { <span class="hljs-comment">// &#x6784;&#x9020;&#x65B9;&#x6CD5;, &#x8C03;&#x7528;new&#x65F6;&#x81EA;&#x52A8;&#x6267;&#x884C;</span>
                <span class="hljs-keyword">this</span>.name = name;
                <span class="hljs-keyword">this</span>.age = age;
            }
            showName() {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`my name is: <span class="hljs-subst">${this.name}</span>`</span>);
            }
            showAge() {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`my age is: <span class="hljs-subst">${this.age}</span>`</span>);
            }
        }
        <span class="hljs-comment">// console.log(typeof Person); // function</span>
        <span class="hljs-keyword">let</span> p1 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">&apos;daliu&apos;</span>, <span class="hljs-number">18</span>);
        p1.showName();
        p1.showAge();
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>&#x7C7B;&#x7684;&#x58F0;&#x660E;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x5F62;&#x5F0F;(&#x4E0D;&#x63A8;&#x8350;):</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Person = <span class="hljs-class"><span class="hljs-keyword">class</span> </span>{
     ...
}
</code></pre>
<h3 id="&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x5C5E;&#x6027;&#x540D;">&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x5C5E;&#x6027;&#x540D;</h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> aaa = <span class="hljs-string">&apos;aaa&apos;</span>;
<span class="hljs-keyword">let</span> bbb = <span class="hljs-string">&apos;bbb&apos;</span>;
<span class="hljs-keyword">let</span> json = {
     [aaa+bbb]:<span class="hljs-string">&apos;welcome to my site!&apos;</span> <span class="hljs-comment">//&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x5C5E;&#x6027;&#x540D;</span>
}
<span class="hljs-built_in">console</span>.log(json);
</code></pre>
<p>class &#x4E2D;&#x4E5F;&#x652F;&#x6301;&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x4E3A;&#x65B9;&#x6CD5;&#x540D;:</p>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let methodName = &apos;myMethodName&apos;;
    class Person {
        constructor(name, age) { // &#x6784;&#x9020;&#x65B9;&#x6CD5;, &#x8C03;&#x7528;new&#x65F6;&#x81EA;&#x52A8;&#x6267;&#x884C;
            this.name = name;
            this.age = age;
        }
        showName() {
            console.log(`my name is: ${this.name}`);
        }
        showAge() {
            console.log(`my age is: ${this.age}`);
        }
        // &#x4E2D;&#x62EC;&#x53F7;&#x4E2D;&#x8868;&#x8FBE;&#x5F0F;&#x8868;&#x793A;&#x540D;&#x5B57;
        [methodName]() {
            console.log(&quot;&#x6D4B;&#x8BD5;&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x5C5E;&#x6027;&#x540D;&#x6216;&#x65B9;&#x6CD5;&#x540D;&quot;);
        }
    }

    // console.log(typeof Person); // function
    let p1 = new Person(&apos;daliu&apos;, 18);
    p1.showName();
    p1.showAge();
    p1[methodName](); // &#x76F8;&#x5F53;&#x4E8E;: p1.myMethodName();
&lt;/script&gt;
</code></pre>
<p>&#x6CE8;: </p>
<ol>
<li>ES6 &#x91CC;&#x9762;class&#x6CA1;&#x6709;&#x63D0;&#x5347;&#x529F;&#x80FD;,&#x5FC5;&#x987B;&#x5148;&#x58F0;&#x660E;&#x518D;&#x4F7F;&#x7528;, ES5&#x4E2D;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x6784;&#x9020;&#x53EF;&#x4EE5;&#x5148;&#x4F7F;&#x7528;&#x518D;&#x58F0;&#x660E;</li>
<li>ES6 &#x4E2D;&#x7684;this&#x6BD4;&#x4E4B;&#x524D;&#x8F7B;&#x677E;&#x5F88;&#x591A;, &#x4F46;&#x5728;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#x4E0B;&#x4F9D;&#x7136;&#x9700;&#x8981;&#x77EB;&#x6B63;</li>
</ol>
<h3 id="&#x77EB;&#x6B63;-this">&#x77EB;&#x6B63; this</h3>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
          <span class="hljs-keyword">constructor</span>() {
              <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&apos;daliu&apos;</span>;

          }
          showName() {
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`this: `</span>, <span class="hljs-keyword">this</span>); <span class="hljs-comment">// this: undefined</span>
          }
      }
      <span class="hljs-keyword">let</span> p1 = <span class="hljs-keyword">new</span> Person();
      <span class="hljs-keyword">let</span> {showName} = p1; <span class="hljs-comment">// &#x4F7F;&#x7528;&#x89E3;&#x6784;</span>
      showName();
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>&#x77EB;&#x6B63; this:</p>
<ol>
<li>fn.call(this&#x6307;&#x5411;&#x8C01;, args1, args2...)</li>
<li>fn.apply(this&#x6307;&#x5411;&#x8C01;, [args1, args2...])</li>
<li>fn.bind()</li>
</ol>
<pre><code class="lang-js">&lt;script&gt;
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
      <span class="hljs-keyword">constructor</span>() {
          <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&apos;daliu&apos;</span>;
          <span class="hljs-keyword">this</span>.showName = <span class="hljs-keyword">this</span>.showName.bind(<span class="hljs-keyword">this</span>); <span class="hljs-comment">// &#x77EB;&#x6B63; this, &#x5F53;&#x8C03;&#x7528; showName &#x65F6;, this&#x6307;&#x4EE3;&#x5F53;&#x524D;&#x5BF9;&#x8C61;</span>
      }
      showName() {
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`this: `</span>, <span class="hljs-keyword">this</span>);
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`this.name: `</span>, <span class="hljs-keyword">this</span>.name);
      }
  }
  <span class="hljs-keyword">let</span> p1 = <span class="hljs-keyword">new</span> Person();
  <span class="hljs-keyword">let</span> {showName} = p1; <span class="hljs-comment">// &#x4F7F;&#x7528;&#x89E3;&#x6784;</span>
  showName();
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="getter-&#x548C;-setter">getter &#x548C; setter</h3>
<pre><code class="lang-js">&lt;script&gt;
    class Person {
        constructor() {
        }
        // setter
        set name(val) {
            console.log(`&#x8BBE;&#x7F6E;name&#x5C5E;&#x6027;&#x7684;&#x503C;&#x4E3A;: ${val}`);
        }
        get name() {
            return &apos;DaLiu&apos;;
        }
    }
    let p1 = new Person();
    p1.name = &apos;123&apos;; //&#x89E3;&#x53D1;set name(&apos;123&apos;)
    console.log(p1.name); //DaLiu
&lt;/script&gt;
</code></pre>
<h3 id="&#x7C7B;&#x65B9;&#x6CD5;&#x9759;&#x6001;&#x65B9;&#x6CD5;">&#x7C7B;&#x65B9;&#x6CD5;(&#x9759;&#x6001;&#x65B9;&#x6CD5;)</h3>
<pre><code class="lang-js">&lt;script&gt;
    class Person {
        constructor() {
        }
        showName() {
            console.log(&apos;this is instance method of showName&apos;);
        }
        static showName() {
            console.log(&apos;this is static method of showName&apos;)
        }
    }
    let p1 = new Person();
    p1.showName(); // this is instance method of showName
    Person.showName(); // this is static method of showName
&lt;/script&gt;
</code></pre>
<h3 id="&#x7EE7;&#x627F;">&#x7EE7;&#x627F;</h3>
<p>&#x5148;&#x56DE;&#x987E;&#x4E00;&#x4E0B;&#x4E4B;&#x524D;&#x7684;&#x7EE7;&#x627F;:</p>
<pre><code class="lang-js">&lt;script&gt;
    <span class="hljs-comment">// &#x7236;&#x7C7B;</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name</span>) </span>{
        <span class="hljs-keyword">this</span>.name = name;
    }
    Person.prototype.showName = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">`&#x540D;&#x5B57;&#x662F;: <span class="hljs-subst">${this.name}</span>`</span>;
    };

    <span class="hljs-comment">// &#x5B50;&#x7C7B;</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Student</span>(<span class="hljs-params">name, action</span>) </span>{
        Person.call(<span class="hljs-keyword">this</span>, name); <span class="hljs-comment">// &#x7EE7;&#x627F;&#x5C5E;&#x6027;</span>
        <span class="hljs-keyword">this</span>.action = action;
    }
    Student.prototype = <span class="hljs-keyword">new</span> Person(); <span class="hljs-comment">// &#x7EE7;&#x627F;&#x65B9;&#x6CD5;</span>

    <span class="hljs-comment">// &#x8C03;&#x7528;</span>
    <span class="hljs-keyword">let</span> stu1 = <span class="hljs-keyword">new</span> Student(<span class="hljs-string">&apos;DaLiu&apos;</span>, <span class="hljs-string">&apos;&#x770B;&#x4E66;&apos;</span>);
    <span class="hljs-built_in">console</span>.log(stu1.name); <span class="hljs-comment">//DaLiu</span>
    <span class="hljs-built_in">console</span>.log(stu1.showName());
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<p>ES6&#x7684;&#x7EE7;&#x627F;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#x6E05;&#x6670;:</p>
<pre><code class="lang-js">&lt;script&gt;
    <span class="hljs-comment">// &#x7236;&#x7C7B;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
        <span class="hljs-keyword">constructor</span>(name) {
            <span class="hljs-keyword">this</span>.name = name;
        }
        showName() {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x7236;&#x7C7B;&#x7684;showName: <span class="hljs-subst">${this.name}</span>`</span>);
        }
    }

    <span class="hljs-comment">// &#x5B50;&#x7C7B;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Person</span> </span>{
        <span class="hljs-comment">/**
        constructor(args) {
            super(args);
        }*/</span>
        <span class="hljs-keyword">constructor</span>(name, action) {
            <span class="hljs-keyword">super</span>(name);
            <span class="hljs-keyword">this</span>.action = action;
        }
        showAction() {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.action);
        }
        showName() {
            <span class="hljs-keyword">super</span>.showName();
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5B50;&#x7C7B;&#x7684;showName: <span class="hljs-subst">${this.name}</span>`</span>);
        }
    }

    <span class="hljs-comment">// &#x8C03;&#x7528;</span>
    <span class="hljs-keyword">let</span> stu1 = <span class="hljs-keyword">new</span> Student(<span class="hljs-string">&apos;DaLiu&apos;</span>, <span class="hljs-string">&apos;&#x770B;&#x4E66;&apos;</span>);
    <span class="hljs-built_in">console</span>.log(stu1.name); <span class="hljs-comment">//DaLiu</span>
    stu1.showName();
    stu1.showAction();
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;, &#x5DE6;&#x53F3;&#x4E24;&#x4E2A;div, &#x5DE6;&#x8FB9;&#x7684;div&#x4F5C;&#x4E3A;&#x7236;&#x7C7B;, &#x4E5F;&#x968F;&#x610F;&#x62D6;&#x62FD;, &#x53F3;&#x8FB9;&#x7684;div&#x4F5C;&#x4E3A;&#x5B50;&#x7C7B;,&#x4E0D;&#x8D85;&#x8FC7;&#x5C4F;&#x5E55;&#x8FB9;&#x754C;&#x62D6;&#x62FD;</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-class">.box</span> {
            <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
            <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
            <span class="hljs-attribute">background</span>: red;
            <span class="hljs-attribute">position</span>: absolute;
            <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
        }
        <span class="hljs-selector-class">.left</span> {
            <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>
        }
        <span class="hljs-selector-class">.right</span> {
            <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;div1&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;box left&quot;</span>&gt;</span>DIV1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;div2&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;box right&quot;</span>&gt;</span>DIV2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-comment">// &#x7236;&#x7C7B; - &#x666E;&#x901A;&#x62D6;&#x62FD;</span>
        <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Drag</span> </span>{
            <span class="hljs-keyword">constructor</span>(id) {
                <span class="hljs-keyword">this</span>.oDiv = <span class="hljs-built_in">document</span>.querySelector(id);
                <span class="hljs-keyword">this</span>.disX = <span class="hljs-number">0</span>;
                <span class="hljs-keyword">this</span>.disY = <span class="hljs-number">0</span>;
                <span class="hljs-keyword">this</span>.init();
            }
            init() {
                <span class="hljs-keyword">this</span>.oDiv.onmousedown = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ev</span>) </span>{
                    <span class="hljs-comment">//alert(this); //&#x5982;&#x679C;&#x4E0D;bind, &#x5219;this&#x5C31;&#x662F;#div1&#x672C;&#x8EAB;</span>
                    <span class="hljs-keyword">this</span>.disX = ev.clientX - <span class="hljs-keyword">this</span>.oDiv.offsetLeft;
                    <span class="hljs-keyword">this</span>.disY = ev.clientY - <span class="hljs-keyword">this</span>.oDiv.offsetTop;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`ev.clientX: <span class="hljs-subst">${ev.clientX}</span>`</span>); <span class="hljs-comment">// touchPont.x &#x70B9;&#x51FB;&#x5904;&#x7684;x(&#x8DDD;&#x7A97;&#x53E3;&#x5DE6;&#x4FA7;&#x8DDD;&#x79BB;)</span>
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`this.oDiv.offsetLeft: <span class="hljs-subst">${this.oDiv.offsetLeft}</span>`</span>); <span class="hljs-comment">// div.left.x div&#x5DE6;&#x4FA7;&#x8DDD;&#x7A97;&#x53E3;&#x8DDD;&#x79BB;</span>

                    <span class="hljs-built_in">document</span>.onmousemove = <span class="hljs-keyword">this</span>.fnMove.bind(<span class="hljs-keyword">this</span>);
                    <span class="hljs-built_in">document</span>.onmouseup = <span class="hljs-keyword">this</span>.fnUp.bind(<span class="hljs-keyword">this</span>);

                    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">//&#x963B;&#x6B62;&#x9ED8;&#x8BA4;&#x4E8B;&#x4EF6;</span>
                }.bind(<span class="hljs-keyword">this</span>);
            }
            fnMove(ev) {
                <span class="hljs-keyword">this</span>.oDiv.style.left = ev.clientX - <span class="hljs-keyword">this</span>.disX + <span class="hljs-string">&apos;px&apos;</span>;
                <span class="hljs-keyword">this</span>.oDiv.style.top = ev.clientY - <span class="hljs-keyword">this</span>.disY + <span class="hljs-string">&apos;px&apos;</span>;
            }
            fnUp(ev) {
                <span class="hljs-built_in">document</span>.onmousemove = <span class="hljs-literal">null</span>;
                <span class="hljs-built_in">document</span>.onmouseup = <span class="hljs-literal">null</span>;
            }
        }

        <span class="hljs-comment">// &#x5B50;&#x7C7B; - &#x9650;&#x5236;&#x62D6;&#x62FD;&#x8303;&#x56F4;</span>
        <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LimitDrag</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Drag</span> </span>{
            fnMove(ev) {
                <span class="hljs-keyword">super</span>.fnMove(ev);
                <span class="hljs-comment">// &#x9650;&#x5236;&#x8303;&#x56F4;(&#x62D6;&#x62FD;&#x65F6;div&#x4E0D;&#x8D85;&#x8FC7;&#x8FB9;&#x754C;)</span>
                <span class="hljs-keyword">let</span> winWidth = <span class="hljs-built_in">document</span>.documentElement.clientWidth;
                <span class="hljs-keyword">let</span> winHeight = <span class="hljs-built_in">document</span>.documentElement.clientHeight;
                <span class="hljs-keyword">let</span> x = ev.clientX - <span class="hljs-keyword">this</span>.oDiv.offsetWidth/<span class="hljs-number">2</span>;
                <span class="hljs-keyword">let</span> y = ev.clientY - <span class="hljs-keyword">this</span>.oDiv.offsetHeight/<span class="hljs-number">2</span>;
                <span class="hljs-keyword">if</span> (x &lt;= <span class="hljs-number">0</span>) {
                    x = <span class="hljs-number">0</span>;
                }
                <span class="hljs-keyword">if</span> ( y &lt;= <span class="hljs-number">0</span>) {
                    y = <span class="hljs-number">0</span>;
                }
                <span class="hljs-keyword">if</span> (x &gt; winWidth - <span class="hljs-keyword">this</span>.oDiv.offsetWidth) {
                    x = winWidth - <span class="hljs-keyword">this</span>.oDiv.offsetWidth;
                }
                <span class="hljs-keyword">if</span> (y &gt; winHeight - <span class="hljs-keyword">this</span>.oDiv.offsetHeight) {
                    y = winHeight - <span class="hljs-keyword">this</span>.oDiv.offsetHeight;
                }
                <span class="hljs-keyword">this</span>.oDiv.style.left = x  + <span class="hljs-string">&apos;px&apos;</span> ;
                <span class="hljs-keyword">this</span>.oDiv.style.top =  y + <span class="hljs-string">&apos;px&apos;</span> ;
            }
        }

        <span class="hljs-keyword">new</span> Drag(<span class="hljs-string">&apos;#div1&apos;</span>);
        <span class="hljs-keyword">new</span> LimitDrag(<span class="hljs-string">&apos;#div2&apos;</span>);
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="模块化.html" class="navigation navigation-prev " aria-label="Previous page: 模块化">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="this.html" class="navigation navigation-next " aria-label="Next page: this">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"class和继承","level":"1.11","depth":1,"next":{"title":"this","level":"1.12","depth":1,"path":"this.md","ref":"this.md","articles":[]},"previous":{"title":"模块化","level":"1.10","depth":1,"path":"模块化.md","ref":"模块化.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"class和继承.md","mtime":"2021-12-14T15:52:09.369Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

